package vtc;

import java.util.concurrent.*;
import java.util.*;

import com.ib.client.Contract;
import com.ib.client.Order;

public class StrategyUnitExampleIB extends FunctionalUnit
{
	StrategyUnitExampleIB(String id, VtcArrayBlockingQueue inQueues[], VtcArrayBlockingQueue outQueues[])
	{
		super(id, inQueues, outQueues);
	}
	
	public void functionalProcess() throws InterruptedException
	{
		Object [] eventv;
		try
		{
			eventv = (Object [])inValue_.getEvent();
		}
		catch(Exception e)
		{
			System.out.print("Input event in StrategyUnitExample not an Object[].");
			return;
		}
		Date timeTag = inValue_.getTimeTag();
		Double price = (Double)eventv[0];
		Double ema = (Double)eventv[1];
		String orderDirection = "BUY";
		if(ema <= price)
		{
			orderDirection ="SELL";
		}
		
		IBContractFactory contractFactory = new IBContractFactory();
		IBOrderFactory orderFactory = new IBOrderFactory();
		Contract contract = contractFactory.makeStockContract("GOOG", "SMART");
		Order order = orderFactory.makeContract(orderDirection, (int)price.longValue());
		
		Map<String, Object> myMap = new HashMap<String, Object>();
		myMap.put("contract",contract);
		myMap.put("order",order);
		
		
		//System.out.print("Transition proposed: " + transition + "\n");
		outValue_ = new VtcTimeTaggedEvent(myMap, timeTag);
	}
}